<?php

class DLDMongoD extends Mongo {

    public $mongodb = '';
    private $collection = '';
    private $database = '';

    public function __construct($host, $port, $database, $options = array("connect" => true)) {
        try {
            //启用连接池，并且不限制连接池的最大个数
            MongoPool::setSize(-1);
            parent::__construct("mongodb://$host:$port", $options);
            if ($options["connect"]) {
                $this->mongodb = $this->selectDB($database);
            }
        } catch (Exception $e) {
            //var_export($e->getMessage());
            throw new Exception("may be mongo server is not started");
        }
        $this->database = $database;
    }

    public function connect() {
        try {
            if (parent::connect()) {
                $this->mongodb = $this->selectDB($this->database);
            }
        } catch (Exception $e) {
            //var_export($e->getMessage());
            return false;
        }

        return $this->connected;
    }

    public function setCollection($collection) {
        $this->collection = $this->mongodb->$collection;
    }

    public function listCollections() {
        $list = $this->mongodb->listCollections();
        return $list;
    }

    public function insert($key_vlaues, $collection, $options=array()) {
        try {
            if ($collection) {
                $this->collection = $this->mongodb->$collection;
            }
            if ($this->collection) {
                return $this->collection->insert($key_vlaues, $options);
            }
        } catch (Exception $e) {
            //var_export($e->getMessage());
            return false;
        }

        return false;
    }

    public function save($key_vlaues, $collection, $options=array()) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->save($key_vlaues, $options);
        }

        return false;
    }

    public function update($condition, $newdata, $collection, $options=array("multiple" => true)) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->update($condition, $newdata, $options);
        }

        return false;
    }

    public function count($query, $collection) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->count($query);
        }

        return 0;
    }

    public function find($query, $collection, $fields = array()) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->find($query, $fields);
        }
        return false;
    }

    public function findOne($query, $collection, $fields = array()) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->findOne($query, $fields);
        }

        return false;
    }

    public function batchInsert($key_vlaues, $collection, $options=array()) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->batchInsert($key_vlaues, $options);
        }

        return false;
    }
    public function group($collection,$keys , array $initial , MongoCode $reduce, array $options = array() ) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->group($keys,$initial, $reduce,$options);
        }

        return false;
    }
    public function remove($query, $collection) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->remove($query);
        }

        return false;
    }

    public function ensureIndex($keys, $collection, $options = array('background' => true)) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->ensureIndex($keys, $options);
        }

        return false;
    }

    public function getIndexInfo($collection) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->getIndexInfo();
        }

        return false;
    }

    public function deleteIndexes($collection) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->deleteIndexes();
        }

        return false;
    }

    public function deleteIndex($keys, $collection) {
        if ($collection) {
            $this->collection = $this->mongodb->$collection;
        }
        if ($this->collection) {
            return $this->collection->deleteIndex($keys);
        }

        return false;
    }

    public function repair() {
        return $this->mongodb->repair();
    }

    public function dropCollection($tbl) {
        $this->mongodb->dropCollection($tbl);
    }

}